var __extends =
    (this && this.__extends) ||
    (function () {
      var e = function (t, i) {
        return (e =
          Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array &&
            function (e, t) {
              e.__proto__ = t;
            }) ||
          function (e, t) {
            for (var i in t)
              Object.prototype.hasOwnProperty.call(t, i) && (e[i] = t[i]);
          })(t, i);
      };
      return function (t, i) {
        function n() {
          this.constructor = t;
        }
        if ("function" != typeof i && null !== i)
          throw new TypeError(
            "Class extends value " + String(i) + " is not a constructor or null"
          );
        e(t, i),
          (t.prototype =
            null === i
              ? Object.create(i)
              : ((n.prototype = i.prototype), new n()));
      };
    })(),
  InviteView = (function (e) {
    function t() {
      var t = e.call(this) || this;
      return (
        (t.isSafari = !1),
        (t.isSync = 0),
        (t.spaceTime = 0),
        t.btn_create.on(Laya.Event.CLICK, t, t.create),
        t.btn_back.on(Laya.Event.CLICK, t, t.onClose),
        t.btn_share.on(Laya.Event.CLICK, t, t.onShare),
        (t.list_invite.renderHandler = new Handler(t, t.onRender)),
        (t.list_invite.selectEnable = !0),
        (t.list_invite.vScrollBarSkin = ""),
        t.list_invite.on(Laya.Event.CLICK, t, t.onClickGetPrize),
        (t.list_invite.scrollBar.changeHandler = new Handler(
          t,
          t.onLoadMoreData
        )),
        t.btn_invite_chaxun.on(Laya.Event.CLICK, t, t.searchInviteData),
        t.label_model.on(Laya.Event.CLICK, t, t.onClickModel),
        t.btn_recharge.on(Laya.Event.CLICK, t, t.onClickRecharge),
        (t.isSafari =
          Browser.userAgent.indexOf("Chrome") < 1 &&
          Browser.userAgent.indexOf("Safari") > -1),
        Browser.onWeiXin,
        t.isSafari && t.createCopyBtn(),
        WebSocketService.addListener({
          caller: t,
          code: 1913,
          method: t.onLoadGetPrize,
        }),
        WebSocketService.addListener({
          caller: t,
          code: 5285,
          method: t.myInviteListSuccess,
        }),
        WebSocketService.addListener({
          caller: t,
          code: 52851,
          method: t.myInviteListSuccess,
        }),
        t
      );
    }
    return (
      __extends(t, e),
      (t.openView = function () {
        return (
          null == this.instance && (this.instance = new t()),
          (this.instance.btn_recharge.visible = !1),
          (this.instance.currentPage = 1),
          (this.instance.label_code.text = ""),
          (this.instance.list_invite.visible = !1),
          this.instance.initShuaixuanData(),
          this.instance.myCode(),
          this.instance
        );
      }),
      (t.prototype.initShuaixuanData = function () {
        var e = new Date(),
          t = e.getFullYear(),
          i = e.getMonth(),
          n = e.getDay();
        this.text_start_year.text =
          t.toString() + "-" + i.toString() + "-" + n.toString();
        var s = e.getFullYear(),
          o = e.getMonth() + 1,
          a = e.getDay();
        this.text_end_year.text =
          s.toString() + "-" + o.toString() + "-" + a.toString();
      }),
      (t.prototype.onClickSearchData = function () {
        var e = Global.roleId,
          t = this.text_start_year.text,
          i = this.text_end_year.text;
        WebSocketService.send({
          c: 52851,
          d: { roleId: e, date1: t, date2: i },
        });
      }),
      (t.prototype.create = function () {
        var e = new Ajax(this);
        (e.success = this.createSuccess),
          e.postForm(
            Environment.apiHost + "/user-service/user/invite/create",
            {}
          );
      }),
      (t.prototype.createSuccess = function (e) {
        if (0 == e.code) {
          this.showBtn(!1), (this.label_code.text = e.data);
          var t = this.isSync;
          (this.isSync = 2), 3 == t && this.wxReady();
        } else ToastView.toastView(e.msg);
      }),
      (t.prototype.searchInviteData = function () {
        WebSocketService.send({ c: 52851, d: { roleId: Global.roleId } });
      }),
      (t.prototype.searchInviteSuccess = function (e) {
        console.log(e);
      }),
      (t.prototype.myCode = function () {
        var e = new Ajax(this);
        (e.success = this.myCodeSuccess),
          e.get(Environment.apiHost + "/user-service/user/invite/my", {});
      }),
      (t.prototype.myCodeSuccess = function (e) {
        if (4027 == e.code)
          return this.showBtn(!0), void (0 == this.isSync && (this.isSync = 1));
        if (0 == e.code) {
          this.showBtn(!1),
            (this.label_code.text = e.data.code),
            1 == e.data.permission && (this.btn_recharge.visible = !0),
            this.myInviteList();
          var t = this.isSync;
          (this.isSync = 2), 3 == t && this.wxReady();
        } else ToastView.toastView(e.msg);
      }),
      (t.prototype.myInviteList = function () {
        var e = new Ajax(this);
        (this.ajaxReq = e),
          (e.success = this.myInviteListSuccess),
          e.post(Environment.apiHost + "/user-service/user/invite/list", {
            page: 1,
            pageSize: 100,
          });
      }),
      (t.prototype.myInviteListSuccess = function (e) {
        if (0 == e.code) {
          var t = e.data;
          if (this.currentPage > 1) {
            if (0 == t.length) return void this.currentPage--;
            for (o = 0; o < t.length; o++) {
              var i = t[o];
              this.listData.push(i), (this.dataSource = []);
            }
          } else (this.listData = t), (this.dataSource = []);
          this.isloading = !1;
          for (var n = this.listData, s = [], o = 0; o < n.length; o++) {
            var a = {
              lbl_date: { text: n[o].createTime.substring(5) },
              lbl_mobile: { text: n[o].mobile },
              lbl_gold: { text: n[o].gold },
              award: n[o].award,
              state: n[o].state,
              id: n[o].id,
            };
            s.push(a);
          }
          (this.list_invite.array = s), (this.list_invite.visible = !0);
        } else ToastView.toastView(e.msg);
      }),
      (t.prototype.onRender = function (e, t) {
        var i = e.dataSource,
          n = e.getChildByName("btn_get");
        i.award == i.state
          ? ((n.label = "已领取"), (n.disabled = !0))
          : ((n.label = "领取"), (n.disabled = !1)),
          i.state > 7 ? (n.visible = !0) : (n.visible = !1);
      }),
      (t.prototype.onClickGetPrize = function (e) {
        if ("btn_get" == e.target.name) {
          var t = e.target.parent.dataSource,
            i = laya.net.LocalStorage.getItem("userId");
          WebSocketService.send({
            c: 1913,
            d: { id: t.id, area: Global.areaId, userId: i },
          });
        }
      }),
      (t.prototype.onLoadGetPrize = function (e) {
        ToastView.toastView(e.msg),
          0 == e.code &&
            (this.myInviteList(),
            Laya.stage.event("GameMainView", "refreshMoney"),
            Laya.stage.event("HomePageView", "refreshMoney"));
      }),
      (t.prototype.weixinApi = function () {
        var e = new Ajax(this),
          t = "url=" + encodeURIComponent(Browser.window.location.href);
        (e.success = this.weixinApiSuccess),
          e.get(
            Environment.apiHost + "/user-service/user/getWeixinJsApiSign",
            t
          );
      }),
      (t.prototype.weixinApiSuccess = function (e) {
        0 != e.code && ToastView.toastView(e.msg);
        var t = Browser.window.wx;
        (e.data.jsApiList = [
          "updateAppMessageShareData",
          "updateTimelineShareData",
        ]),
          t.ready(this.wxReady),
          t.error(function (e) {
            console.log(e);
          }),
          t.config(e.data);
      }),
      (t.prototype.loadJS = function (e) {
        var t = Browser.document,
          i = t.createElement("script");
        (i.type = "text/javascript"),
          (i.onload = this.onLoadJS),
          (i.src = e),
          t.getElementsByTagName("head")[0].appendChild(i);
      }),
      (t.prototype.onLoadJS = function () {
        t.instance.weixinApi();
      }),
      (t.prototype.wxReady = function () {
        var e = t.instance.isSync;
        if (((t.instance.isSync = 3), 2 == e)) {
          var i = Browser.window.jWeixin,
            n = Environment.shareUrl + "?c=" + t.instance.label_code.text;
          i.updateAppMessageShareData({
            title: "前缘三国",
            desc: "邀请你加入游戏一起金戈铁马征战沙场",
            link: n,
            imgUrl: Environment.shareUrl + "favicon.jpg",
            trigger: t.shareSuccess,
          }),
            i.updateTimelineShareData({
              title: "前缘三国",
              link: n,
              imgUrl: Environment.shareUrl + "favicon.jpg",
              success: function () {
                ToastView.toastView("加载成功");
              },
              trigger: t.shareSuccess,
            });
        }
      }),
      (t.prototype.showBtn = function (e) {
        e
          ? ((this.btn_create.visible = !0),
            this.isSafari && this.copyBtn
              ? (this.copyBtn.hidden = !0)
              : (this.btn_share.visible = !1))
          : ((this.btn_create.visible = !1),
            this.isSafari && this.copyBtn
              ? (this.copyBtn.hidden = !1)
              : (this.btn_share.visible = !0));
      }),
      (t.prototype.onShare = function () {
        this.onCopy();
      }),
      (t.shareSuccess = function () {
        (t.instance.label_model.visible = !1),
          (t.instance.label_share.visible = !1),
          ToastView.toastView("分享成功");
      }),
      (t.prototype.onClickModel = function () {
        (this.label_model.visible = !1), (this.label_share.visible = !1);
      }),
      (t.prototype.onCopy = function () {
        var e =
            Environment.shareUrl + "index.html?c=" + t.instance.label_code.text,
          i = Browser.document,
          n = (Browser.window, i.createElement("input"));
        n.setAttribute("value", e),
          i.body.appendChild(n),
          (n.style.cssText =
            "z-index:100001;position: absolute; left: 0px; top: 0px;");
        var s = n.hasAttribute("readonly");
        s || n.setAttribute("readonly", ""),
          n.select(),
          n.setSelectionRange(0, e.length),
          s || n.removeAttribute("readonly");
        Browser.document.execCommand("copy");
        i.body.removeChild(n), ToastView.toastView("已复制到粘贴板");
      }),
      (t.prototype.createCopyBtn = function () {
        var e = Browser.document,
          i = (Browser.window, e.createElement("button"));
        i.setAttribute("type", "button"),
          (i.innerHTML = "复制"),
          (i.id = "sgBtnCopy"),
          (i.style.cssText =
            "z-index:100001;position: absolute; left: 275px; top: 84px;height:25px;width:50;font-size:16px"),
          (i.hidden = !0),
          e.body.appendChild(i),
          (i.onclick = function () {
            t.instance.onCopy();
          }),
          (this.copyBtn = i);
      }),
      (t.prototype.onClickRecharge = function () {
        var e = InviteRechargeView.openView();
        e.pos(0, 35), Laya.stage.addChild(e);
      }),
      (t.prototype.onLoadMoreData = function (e) {
        var t = e / this.list_invite.scrollBar.max,
          i = new Date(),
          n = !1;
        if (
          (i.getTime() - this.spaceTime >= 3e3 && (n = !0),
          1 == t && 0 == this.isloading && n)
        ) {
          (this.isloading = !0), this.currentPage++;
          var s = new Ajax(this),
            o = { page: this.currentPage, pageSize: 100 };
          return (
            (this.ajaxReq = s),
            (s.success = this.myInviteListSuccess),
            s.post(Environment.apiHost + "/user-service/user/invite/list", o),
            void (this.spaceTime = i.getTime())
          );
        }
      }),
      (t.prototype.onClose = function () {
        this.copyBtn && (this.copyBtn.hidden = !0), this.removeSelf();
      }),
      t
    );
  })(ui.InviteController.InviteViewUI);
